package com.kye.express.business.customer_intro.add;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.express.databinding.ActivityCustomerIntroAddBinding;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.customer_intro.AddCustomerIntroResultBean;
import com.kye.model.bean.customer_intro.GetCashCountResultBean;
import com.kye.model.bean.customer_intro.GetIntroInfoResultBean;
import com.kye.model.bean.customer_intro.IntroCompyModel;
import com.kye.model.bean.customer_intro.RequestAddCustomerIntroBean;
import com.kye.model.bean.customer_intro.RequestGetCashCountBean;
import com.kye.model.bean.customer_intro.RequestGetIntroInfoBean;
import com.kye.model.bean.user.User;
import com.kye.model.constant.Constants;
import com.kye.model.service.CustomerIntroService;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface CustomerIntroAddActivityContract {

    interface CustomerIntroAddActivityView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(AddCustomerIntroResultBean addCustomerIntroResultBean);

        void onOtherError(String msg);

        void onApiError(String msg);

        void onLoadedGetInfo(GetIntroInfoResultBean resultBean);
    }

    class CustomerIntroAddActivityViewPresenter extends BasePresenter<CustomerIntroAddActivityContract.CustomerIntroAddActivityView> {

        private Context mContext;

        public CustomerIntroAddActivityViewPresenter(Context context) {
            this.mContext = context;
        }


        public void addCustomerIntro(ActivityCustomerIntroAddBinding binding, List<IntroCompyModel> introCompyModels, String guid) {
            String phone = "";
            String username = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
                username = user.getUserName();
            }

            RequestAddCustomerIntroBean requestAddCustomerIntroBean = new RequestAddCustomerIntroBean();
            if (!TextUtils.isEmpty(guid)) {
                requestAddCustomerIntroBean.setSysGuid(guid);
            }
            requestAddCustomerIntroBean.setPhone(phone);
            requestAddCustomerIntroBean.setIntroductionToCompany(binding.myIntroduceEditCompany.getText().toString());
            requestAddCustomerIntroBean.setContactPeople(binding.myIntroduceEditName.getText().toString());
            requestAddCustomerIntroBean.setContactTelephone(binding.myIntroduceEditPhone.getText().toString());
            requestAddCustomerIntroBean.setContactAddress(binding.myIntroduceTxtArea.getText().toString());
            requestAddCustomerIntroBean.setContactPost(binding.myIntroduceEditPsition.getText().toString());
            requestAddCustomerIntroBean.setIntroductionPeople(username);
            requestAddCustomerIntroBean.setExplain(binding.introduceEditDesc.getText().toString());
            requestAddCustomerIntroBean.setCustomerSources(0);
            requestAddCustomerIntroBean.setHaveIntroduced(binding.rbIntroYes.isChecked() ? 1 : 0);
            requestAddCustomerIntroBean.setNoIntroduction(binding.rbIntroNo.isChecked() ? 1 : 0);
            requestAddCustomerIntroBean.setIsFriend(binding.rbFriend.isChecked() ? 1 : 0);
            requestAddCustomerIntroBean.setIsColleague(binding.rbColleague.isChecked() ? 1 : 0);
            requestAddCustomerIntroBean.setIsFamily(binding.rbBusiness.isChecked() ? 1 : 0);
            requestAddCustomerIntroBean.setIsPersonInCharge(binding.rbPrincipalYes.isChecked() ? 1 : 0);
            requestAddCustomerIntroBean.setNotPersonInCharge(binding.rbPrincipalNo.isChecked() ? 1 : 0);
            String otherString = binding.edtOther.getText().toString();
            if (TextUtils.isEmpty(otherString)) {
                requestAddCustomerIntroBean.setIsSendOther(0);
            } else {
                requestAddCustomerIntroBean.setIsSendOther(1);
                requestAddCustomerIntroBean.setSendOther(otherString);
            }

            if (introCompyModels.size() < 10) {
                return;
            }
            //TODO 如果资源文件中的顺序换了，这里的position也需要换
            requestAddCustomerIntroBean.setIsSendSF(introCompyModels.get(0).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendBD(introCompyModels.get(1).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendLB(introCompyModels.get(2).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendSE(introCompyModels.get(3).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendYD(introCompyModels.get(4).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendYT(introCompyModels.get(5).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendST(introCompyModels.get(6).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendZT(introCompyModels.get(7).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendBS(introCompyModels.get(8).isCheck() ? 1 : 0);
            requestAddCustomerIntroBean.setIsSendAN(introCompyModels.get(9).isCheck() ? 1 : 0);

            String method = "";
            if (TextUtils.isEmpty(guid)) {
                method = "client_newCustomers";
            } else {
                method = "client_introduceModify";
            }
            RetrofitManager.getInstance().createOpenApiService(CustomerIntroService.class)
                    .addCustomerIntro(method, requestAddCustomerIntroBean)
                    .compose(getView().<OpenApiResult<AddCustomerIntroResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<AddCustomerIntroResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<AddCustomerIntroResultBean> result, AddCustomerIntroResultBean datas) {
                            getView().onLoaded(datas);
                        }

                        @Override
                        public void onApiError(OpenApiResult<AddCustomerIntroResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onApiError(msg);
                            } else {
                                getView().onOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }

        public void getIntroInfo(String sysGuid) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(CustomerIntroService.class)
                    .getIntroInfo(new RequestGetIntroInfoBean(phone, sysGuid))
                    .compose(getView().<OpenApiResult<GetIntroInfoResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<GetIntroInfoResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<GetIntroInfoResultBean> result, GetIntroInfoResultBean datas) {
                            if (datas != null) {
                                getView().onLoadedGetInfo(datas);
                            }else {
                                getView().onOtherError(mContext.getString(R.string.common_no_datas));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<GetIntroInfoResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onApiError(msg);
                            } else {
                                getView().onOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }
    }
}
